package com.orange.core.service.credit;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.mapper.credit.CreditFlowMapper;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.entity.credit.CreditFlow;
import com.orange.core.pojo.enums.EFlowBusinessType;
import com.orange.core.util.PrincipalUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CreditFlowService extends ServiceImpl<CreditFlowMapper, CreditFlow> {

    public Page<CreditFlow> page(PageReqDto<Void> dto) {
        Page<CreditFlow> page = this.lambdaQuery()
                .eq(CreditFlow::getUserId, PrincipalUtil.getId())
                .orderByDesc(CreditFlow::getId)
                .page(dto.toPage());
        List<CreditFlow> creditFlows = page.getRecords();
        if (CollUtil.isNotEmpty(creditFlows)) {
            creditFlows.forEach(creditFlow -> creditFlow.setBusinessName(EFlowBusinessType.getName(creditFlow.getBusinessType())));
        }
        return page;
    }
}
